/**
 * 倒计时指令。在dom元素上加上这个标签，可以实现倒计时，disabled等判定
 */
APP.directive('sendSms', ['$timeout', '$stateParams', 'AreaService', '$state',
  function ($timeout, $stateParams, AreaService, $state) {
    return {
      restrict: 'A',
      link: function (scope, element, attr) {
        var countMax = attr.sendSms || 5;
        var count = countMax;
        var textReady = '点击发送验证码';
        var textCount = '秒后重新发送';
        element.on('click', function () {
          element[0].disabled = true;
          element[0].innerText = count + textCount;
          var timerId = setInterval(function () {
            if (count > 0) {
              count = count - 1;
              element[0].innerText = count + textCount;
            } else {
              count = countMax;
              element[0].disabled = false;
              element[0].innerText = textReady;
              clearInterval(timerId);//停止计时
            }
          }, 1000);
        });
      }
    }
  }]);
